var app = getApp(), api = require("../../api.js"), QQMapWX = require("../../utils/qqmap-wx-jssdk.js");
var images = require('../../utils/images.js');
var qqmapsdk;
var arrayArea1 = [];
var arrayArea1s = [];
var arrayArea2 = [];
var arrayArea2s = [];
var arrayArea3 = [];
var arrayArea3s = [];
var arrayArea4 = [];
var arrayArea4s = [];
Page({
  data: {
    infoType: '自营商家',
    fee: '0.00',
    deposit: '0.00',
    codeFlag: true,
    codeContent: '获取验证码',
    second: 60,
    codeMsg: 10086,
    info: {
      id: '',
      nowAddress: '',
      detailAddress: '',
      lng: '',
      lat: '',
      phone: '',
      code: '',
      name: '',
      shop: '',
      tel: '',
      flag1: !1,
      positiveID: images.common.addImageIcon,
      flag2: !1,
      sideID: images.common.addImageIcon,
      flag3: !1,
      businessLicense: images.common.addImageIcon,
      flag4: !1,
      foodLicense: images.common.addImageIcon,
      province_id:0,
      city_id:0,
      district_id:0,
      street_id:0
    },
    area1Name:"- 请选择 -",
    area2Name: "- 请选择 -",
    area3Name: "- 请选择 -",
    area4Name: "- 请选择 -",
    display2:"none",
    display3:"none",
    display4:"none",
    arrayArea1:[],
    arrayArea2: [],
    arrayArea3:[],
    arrayArea4:[]
  },
  onLoad: function (t) {
    var a = this;
    console.log(api.mch.apply_submit);
    qqmapsdk = new QQMapWX({
      key: 'XALBZ-UOZCI-PHQG7-5CBUT-JH6HQ-GWFES'
    });
    wx.showLoading({
      title: "加载中"
    }), wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success: function (res) {
              a.getMyLocation();
            },
            complete: function () {
              wx.hideLoading();
            }
          });
        } else {
          a.getMyLocation();
        }
      }
    });
    app.request({
      url: api.mch.rule,
      success: function (d) {
        if (t.type == 0) {
          a.data.info.id = 1;
          a.setData({
            infoType: '自营商家',
            info: a.data.info,
            fee: d.data.bdr_enter,
            deposit: d.data.bdy_enter
          })
        } else if (t.type == 1) {
          a.data.info.id = 2;
          a.setData({
            infoType: '附近商家',
            info: a.data.info,
            fee: d.data.br_enter,
            deposit: d.data.by_enter
          })
        } else if (t.type == 2) {
          a.data.info.id = 3;
          a.setData({
            infoType: '特产美食',
            info: a.data.info,
            fee: d.data.tr_enter,
            deposit: d.data.ty_enter
          })
        }
      },
      fail: function (d) {
        wx.showToast({
          title: '获取规则失败',
          image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
        });
      }
    });
   // api.default.district2 = "http://www.wyc.com/addons/zjhj_mall/core/web/index.php?store_id=1&r=api/default/district2";
    app.request({
      url: api.default.district2+"&area_id=0",
      success: function (d) {
        console.log(d.data);
        var list =d.data;
        var object = new Object();
       
        for(var key in list){
          object = list[key].name;
          arrayArea1.push(object);
          var objs = new Object();
          objs.id = list[key].id;
          objs.name = list[key].name;
          arrayArea1s.push(objs);
        }
        a.setData({ arrayArea1: arrayArea1});
      },
      fail: function (d) {
        wx.showToast({
          title: '获取规则省数据失败',
          image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
        });
      }
    });
  },
  onReady: function () { },
  onShow: function () { },
  onHide: function () { },
  onUnload: function () { },
  onPullDownRefresh: function () { },
  onReachBottom: function () { },
  onShareAppMessage: function () { },
  getMyLocation: function () {
    var a = this;
    wx.getLocation({
      success: function (t) {
        (a.data.info.lng = t.longitude, a.data.info.lat = t.latitude) && a.setData({
          info: a.data.info
        });
        qqmapsdk.reverseGeocoder({
          location: {
            latitude: t.latitude,
            longitude: t.longitude
          },
          success: function (addressRes) {
            var address = addressRes.result.formatted_addresses.recommend;
            a.data.info.nowAddress = address, a.setData({
              info: a.data.info
            })
          },
          fail: function (res) {
            wx.showToast({
              title: '获取位置失败'
            });
          },
          complete: function () {
            wx.hideLoading();
          }
        });
      }
    });
  },
  goto: function (a) {
    var e = this;
    wx.getSetting({
      success: function (t) {
        t.authSetting["scope.userLocation"] ? e.location(a) : app.getauth({
          content: "需要获取您的地理位置授权，请到小程序设置中打开授权！",
          cancel: !1,
          success: function (t) {
            t.authSetting["scope.userLocation"] && e.location(a);
          }
        });
      }
    });
  },
  location: function (t) {
    var a = this;
    wx.chooseLocation({
      success: function (res) {
        (a.data.info.lng = res.longitude, a.data.info.lat = res.latitude, a.data.info.nowAddress = res.name) && a.setData({
          info: a.data.info
        });
      },
      fail: function (res) {
        wx.showToast({
          title: '选择位置失败'
        })
      }
    });
  },
  getAddress: function (e) {
    var t = this;
    var val = e.detail.value;
    t.data.info.detailAddress = val;
    t.setData({
      info: t.data.info
    });
  },
  getPhone: function (e) {
    var t = this;
    var val = e.detail.value;
    t.data.info.phone = val;
    t.setData({
      info: t.data.info
    });
  },
  postCode: function (e) {
    var t = this;
    var flag = t.data.codeFlag;
  },
  postCode: function () {
    var t = this, n = this.data.info.phone, flag = t.data.codeFlag;
    console.log(n);
    flag ? (/^1[34578]\d{9}$/.test(n) && 11 == n.length ? app.request({
      url: api.user.user_hand_binding,
      method: "POST",
      data: {
        content: n
      },
      success: function (n) {
        console.log(n.msg);
        0 == n.code ? (t.timer(), t.setData({
          codeFlag: !1,
          codeMsg: n.msg
        })) : (n.code, wx.showToast({
          title: n.msg,
          image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
        }));
      }
    }) : wx.showToast({
      title: "手机号码错误",
      image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
    })) : console.log(flag);
  },
  timer: function () {
    var a = this;
    new Promise(function (n, t) {
      var e = setInterval(function () {
        a.setData({
          second: a.data.second - 1,
          codeContent: '还剩' + a.data.second + 's'
        }), a.data.second <= 0 && (a.setData({
          codeFlag: !0,
          second: 60,
          codeContent: '重新获取'
        }), n(e));
      }, 1e3);
    }).then(function (n) {
      clearInterval(n);
    });
  },
  getCode: function (e) {
    var t = this;
    var val = e.detail.value;
    t.data.info.code = val;
    t.setData({
      info: t.data.info
    });
  },
  getName: function (e) {
    var t = this;
    var val = e.detail.value;
    t.data.info.name = val;
    t.setData({
      info: t.data.info
    });
  },
  getShop: function (e) {
    var t = this;
    var val = e.detail.value;
    t.data.info.shop = val;
    t.setData({
      info: t.data.info
    });
  },
  getTel: function (e) {
    var t = this;
    var val = e.detail.value;
    t.data.info.tel = val;
    t.setData({
      info: t.data.info
    });
  },
  uploadPositiveID: function () {
    var t = this;
    app.uploader.upload({
      start: function (a) {
        wx.showLoading({
          title: "正在上传",
          mask: !0
        })
      },
      success: function (a) {
        0 == a.code ? (t.data.info.positiveID = a.data.url, t.data.info.flag1 = !0, t.setData({
          info: t.data.info
        })) : wx.showToast({
          title: a.msg
        });
        console.log(t.data.info);
      },
      error: function (a) {
        console.log(a), wx.showToast({
          title: a
        })
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },
  uploadSideID: function () {
    var t = this;
    app.uploader.upload({
      start: function (a) {
        wx.showLoading({
          title: "正在上传",
          mask: !0
        })
      },
      success: function (a) {
        0 == a.code ? (t.data.info.sideID = a.data.url, t.data.info.flag2 = !0, t.setData({
          info: t.data.info
        })) : wx.showToast({
          title: a.msg
        });
        console.log(t.data.info);
      },
      error: function (a) {
        console.log(a), wx.showToast({
          title: a
        })
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },
  uploadBusinessLicense: function () {
    var t = this;
    app.uploader.upload({
      start: function (a) {
        wx.showLoading({
          title: "正在上传",
          mask: !0
        })
      },
      success: function (a) {
        0 == a.code ? (t.data.info.businessLicense = a.data.url, t.data.info.flag3 = !0, t.setData({
          info: t.data.info
        })) : wx.showToast({
          title: a.msg
        });
        console.log(t.data.info);
      },
      error: function (a) {
        console.log(a), wx.showToast({
          title: a
        })
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },
  uploadFoodLicense: function () {
    var t = this;
    app.uploader.upload({
      start: function (a) {
        wx.showLoading({
          title: "正在上传",
          mask: !0
        })
      },
      success: function (a) {
        0 == a.code ? (t.data.info.foodLicense = a.data.url, t.data.info.flag4 = !0, t.setData({
          info: t.data.info
        })) : wx.showToast({
          title: a.msg
        });
        console.log(t.data.info);
      },
      error: function (a) {
        console.log(a), wx.showToast({
          title: a
        })
      },
      complete: function () {
        wx.hideLoading()
      }
    })
  },
  bindArea1Change: function (e) {
    var a = this;
    var index = e.detail.value;
   // console.log(index, arrayArea1s[index]['name'], arrayArea1s);
   // console.log(this.data.info);
    a.setData({
      area1Name: arrayArea1s[index]['name'], 
      display2: "none", area2Name:"- 请选择 - ",
      display3: "none", area3Name: "- 请选择 - ",
      display4: "none",area4Name: "- 请选择 - ",
      arrayArea2:[]
    });
    a.data.info.province_id=arrayArea1s[index]['id'];
    a.data.info.city_id = 0;
    a.data.info.district_id = 0;
    a.data.info.street_id = 0;
    app.request({
      url: api.default.district2 + "&area_id=" + a.data.info.province_id,
      success: function (d) {
        console.log(d.data);
        var list = d.data;
        var object = new Object();
        arrayArea2= []; 
        arrayArea2s = [];
        for (var key in list) {
          object = list[key].name;
          arrayArea2.push(object);
          var objs = new Object();
          objs.id = list[key].id;
          objs.name = list[key].name;
          arrayArea2s.push(objs);
        }
        a.setData({ arrayArea2: arrayArea2, display2:"" });
      },
      fail: function (d) {
        wx.showToast({
          title: '获取规则市数据失败',
          image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
        });
      }
    });
  },
  bindArea2Change: function (e) {
    var a = this;
    var index = e.detail.value;
    a.setData({
      area2Name: arrayArea2s[index]['name'],
      display2: "",
      display3: "none", area3Name:"- 请选择 -",
      display4: "none",area4Name: "- 请选择 -",
      arrayArea3: []
    });
    arrayArea3 = [];
    arrayArea3s = [];
    a.data.info.city_id = arrayArea2s[index]['id'];
    a.data.info.district_id = 0;
    a.data.info.street_id = 0;
    app.request({
      url: api.default.district2 + "&area_id=" + a.data.info.city_id,
      success: function (d) {
        var list = d.data;
        var object = new Object();
        for (var key in list) {
          object = list[key].name;
          arrayArea3.push(object);
          var objs = new Object();
          objs.id = list[key].id;
          objs.name = list[key].name;
          arrayArea3s.push(objs);
        }
        a.setData({ arrayArea3: arrayArea3,display3:"" });
      },
      fail: function (d) {
        wx.showToast({
          title: '获取规则县数据失败',
          image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
        });
      }
    });
  },
  bindArea3Change: function (e) {
    var a = this;
    var index = e.detail.value;
    a.setData({
      area3Name: arrayArea3s[index]['name'],
      display2: "",
      display3: "",
      display4: "none", area4Name: "- 请选择 -",
      arrayArea4: []
    });
    arrayArea4 = [];
    arrayArea4s = [];
    a.data.info.district_id = arrayArea3s[index]['id'];
    a.data.info.street_id = 0;
    app.request({
      url: api.default.district2 + "&area_id=" + a.data.info.district_id,
      success: function (d) {
        var list = d.data;
        var object = new Object();
        for (var key in list) {
          object = list[key].name;
          arrayArea4.push(object);
          var objs = new Object();
          objs.id = list[key].id;
          objs.name = list[key].name;
          arrayArea4s.push(objs);
        }
        a.setData({ arrayArea4: arrayArea4, display4: "" });
      },
      fail: function (d) {
        wx.showToast({
          title: '获取规则县数据失败',
          image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
        });
      }
    });
  },
  bindArea4Change: function (e) {
    var a = this;
    var index = e.detail.value;
    a.setData({
      area4Name: arrayArea4s[index]['name'],
      display2: "",
      display3: "",
      display4: ""
    });
    a.data.info.street_id = arrayArea4s[index]['id'];
  },
  submitInfo: function () {
    var t = this;
    var info = t.data.info;
    var myData = {};
    app.showTip(info.province_id, '请选择所在地区') && app.showTip(info.city_id, '请选择所在城市') && app.showTip(info.district_id, '请选择所在区/县') && app.showTip(info.street_id, '请选择所在街道') && app.showTip(info.nowAddress, '请选择地址') && app.showTip(info.detailAddress, '请输入详细地址') && app.showTip(info.phone, '请输入手机号码') && app.checkPhone(info.phone) && app.showTip(info.code, '请输入验证码') && app.checkCode(info.code, t.data.codeMsg) && app.showTip(info.name, '请输入真实姓名') && app.showTip(info.shop, '请输入店铺名称') && app.showTip(info.flag1, '身份证正面缺失') && app.showTip(info.flag2, '身份证反面缺失') && app.showTip(info.flag3, '营业执照缺失') && (myData = { realname: info.name, tel: info.phone, name: info.shop, shop_tel: info.tel, sfz: info.positiveID, sfz_s: info.sideID, yyzz: info.businessLicense, spzz: info.foodLicense, mch_common_cat_id: info.id, address: info.nowAddress + info.detailAddress, lat: info.lat, log: info.lng,province_id:info.province_id,city_id:info.city_id,district_id:info.district_id,street_id:info.street_id }, app.request({
      url: api.mch.apply_submit,
      method: 'POST',
      data: myData,
      success: function (d) {
        console.log(d);
        if (d.code == 0) {
          wx.redirectTo({
            url: '/pages/admission-result/admission-result?status=0',
          });
        } else {
          wx.showToast({
            title: d.msg,
            image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
          });
        }

      },
      fail: function (d) {
        wx.showToast({
          title: '提交申请失败',
          image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
        });
      },
      complete: function (d) {
        wx.hideLoading();
        console.log(myData);
      }
    }));


  }
});